package org.brakepedal.server.support.http;

import java.util.Objects;

/**
 * Date: 2023/2/13 13:56

 
 */
public interface EnumConverter<K,V> {
    V getValue();

    default K getKey() {
        return null;
    }

    default boolean hasSameValue(Object value) {
        Objects.requireNonNull(value);
        if(value instanceof EnumConverter<?,?>) {
            return value.equals(this);
        }
        return value.equals(getValue());
    }

    static <T extends Enum<T>> T nameOf(Class<T> enumClass,
                                         String name) {
        T enumInstance;
        try {
            enumInstance = Enum.valueOf(enumClass, name.toLowerCase());
        } catch (Exception ignore) {
            enumInstance = Enum.valueOf(enumClass, name.toUpperCase());
        }
        return enumInstance;
    }
}
